/**
 * Copyright (C) 2010 David Simonek <dafesimonek@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ConcertHeaderPanel.java
 *
 * Created on Aug 6, 2010, 9:28:26 PM
 */

package org.dafe.partyband.gui.concerts;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import org.dafe.partyband.db.api.IConcert;
import org.dafe.partyband.db.api.handlers.IConcertHandler;
import org.dafe.partyband.db.api.DbManager;
import org.openide.util.NbBundle;
import org.openide.windows.TopComponent;

/**
 *
 * @author dafe
 */
public class ConcertHeaderPanel extends javax.swing.JPanel {
    private final IConcert concert;
    private static final IConcertHandler handler = DbManager.getBandManager().getHandlersLookup().lookup(IConcertHandler.class);
    private final TopComponent parentTc;

    /** Creates new form ConcertHeaderPanel */
    public ConcertHeaderPanel(IConcert concert, TopComponent parentTc) {
        initComponents();
        this.concert = concert;
        this.parentTc = parentTc;

        datePicker.setDate(concert.getDate());
        if (concert.getStartHour() != null) {
            DateFormat df = DateFormat.getTimeInstance(DateFormat.SHORT);
            startHourField.setText(df.format(concert.getStartHour()));
        }
        datePicker.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                updateTitle();
            }
        });
        locationField.setText(concert.getLocation());
        locationField.getDocument().addDocumentListener(new DocumentListener() {

            public void insertUpdate(DocumentEvent e) {
                updateTitle();
            }

            public void removeUpdate(DocumentEvent e) {
                updateTitle();
            }

            public void changedUpdate(DocumentEvent e) {
                updateTitle();
            }
        });
        if (concert.getDescription() != null) {
            descriptionField.setText(concert.getDescription());
        }

        updateTitle();
    }

    private void updateTitle () {
        StringBuilder sb = new StringBuilder(
                NbBundle.getMessage(ConcertTopComponent.class, "CTL_ConcertTopComponent"));
        sb.append(" ").append(locationField.getText());
        DateFormat di = SimpleDateFormat.getDateInstance();
        sb.append("-").append(di.format(datePicker.getDate()));
        parentTc.setName(sb.toString());
    }

    public void storeData () {
        concert.setDate(datePicker.getDate());
        concert.setLocation(locationField.getText());
        concert.setDescription(descriptionField.getText());

        DateFormat ti = SimpleDateFormat.getTimeInstance(DateFormat.SHORT);
        Date result = null;
        try {
            result = ti.parse(startHourField.getText());
        } catch (ParseException ex) {
            // no op
        }
        if (result != null) {
            concert.setStartHour(result);
        }

        handler.storeConcert(concert);
    }

    public String getConcertLocation () {
        return locationField.getText();
    }

    public String getConcertDate () {
        return SimpleDateFormat.getDateInstance().format(datePicker.getDate());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jXLabel1 = new org.jdesktop.swingx.JXLabel();
        datePicker = new org.jdesktop.swingx.JXDatePicker();
        jXLabel2 = new org.jdesktop.swingx.JXLabel();
        startHourField = new javax.swing.JTextField();
        jXLabel3 = new org.jdesktop.swingx.JXLabel();
        jXLabel4 = new org.jdesktop.swingx.JXLabel();
        locationField = new javax.swing.JTextField();
        jXLabel5 = new org.jdesktop.swingx.JXLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        descriptionField = new javax.swing.JTextArea();

        jXLabel1.setText(org.openide.util.NbBundle.getMessage(ConcertHeaderPanel.class, "ConcertHeaderPanel.jXLabel1.text", new Object[] {})); // NOI18N
        jXLabel1.setToolTipText(org.openide.util.NbBundle.getMessage(ConcertHeaderPanel.class, "ConcertHeaderPanel.jXLabel1.toolTipText", new Object[] {})); // NOI18N

        jXLabel2.setText(org.openide.util.NbBundle.getMessage(ConcertHeaderPanel.class, "ConcertHeaderPanel.jXLabel2.text", new Object[] {})); // NOI18N
        jXLabel2.setToolTipText(org.openide.util.NbBundle.getMessage(ConcertHeaderPanel.class, "ConcertHeaderPanel.jXLabel2.toolTipText", new Object[] {})); // NOI18N

        startHourField.setText(org.openide.util.NbBundle.getMessage(ConcertHeaderPanel.class, "ConcertHeaderPanel.startHourField.text", new Object[] {})); // NOI18N
        startHourField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                startHourFieldActionPerformed(evt);
            }
        });

        jXLabel3.setText(org.openide.util.NbBundle.getMessage(ConcertHeaderPanel.class, "ConcertHeaderPanel.jXLabel3.text", new Object[] {})); // NOI18N

        jXLabel4.setText(org.openide.util.NbBundle.getMessage(ConcertHeaderPanel.class, "ConcertHeaderPanel.jXLabel4.text", new Object[] {})); // NOI18N
        jXLabel4.setToolTipText(org.openide.util.NbBundle.getMessage(ConcertHeaderPanel.class, "ConcertHeaderPanel.jXLabel4.toolTipText", new Object[] {})); // NOI18N

        locationField.setText(org.openide.util.NbBundle.getMessage(ConcertHeaderPanel.class, "ConcertHeaderPanel.locationField.text", new Object[] {})); // NOI18N
        locationField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                locationFieldActionPerformed(evt);
            }
        });

        jXLabel5.setText(org.openide.util.NbBundle.getMessage(ConcertHeaderPanel.class, "ConcertHeaderPanel.jXLabel5.text", new Object[] {})); // NOI18N
        jXLabel5.setToolTipText(org.openide.util.NbBundle.getMessage(ConcertHeaderPanel.class, "ConcertHeaderPanel.jXLabel5.toolTipText", new Object[] {})); // NOI18N

        descriptionField.setColumns(26);
        descriptionField.setRows(3);
        jScrollPane1.setViewportView(descriptionField);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jXLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jXLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jXLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(datePicker, javax.swing.GroupLayout.PREFERRED_SIZE, 161, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jXLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(startHourField, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jXLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(locationField, javax.swing.GroupLayout.DEFAULT_SIZE, 361, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 361, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jXLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(datePicker, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jXLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(startHourField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jXLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jXLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(locationField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jXLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void startHourFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_startHourFieldActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_startHourFieldActionPerformed

    private void locationFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_locationFieldActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_locationFieldActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private org.jdesktop.swingx.JXDatePicker datePicker;
    private javax.swing.JTextArea descriptionField;
    private javax.swing.JScrollPane jScrollPane1;
    private org.jdesktop.swingx.JXLabel jXLabel1;
    private org.jdesktop.swingx.JXLabel jXLabel2;
    private org.jdesktop.swingx.JXLabel jXLabel3;
    private org.jdesktop.swingx.JXLabel jXLabel4;
    private org.jdesktop.swingx.JXLabel jXLabel5;
    private javax.swing.JTextField locationField;
    private javax.swing.JTextField startHourField;
    // End of variables declaration//GEN-END:variables

}
